package com.risetechnology.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.risetechnology.common.Shared;
import com.risetechnology.data.RecipeUpdate;
import com.risetechnology.krystalkitchen1.ActivityRecipeUpdateDescription;
import com.risetechnology.krystalkitchen1.ActivityUpdate;
import com.risetechnology.krystalkitchen1.R;
import com.risetechnology.master.NavigationActivity;
import com.risetechnology.utils.threads.DownloadRecipeTask;

public class RecipeUpdateListAdapter extends ArrayAdapter<RecipeUpdate> {

	private Context context;
	private int layoutId;
	private List<RecipeUpdate> data = null;
	private RecipeUpdate firstRecipe = new RecipeUpdate();

	public RecipeUpdateListAdapter(Context context, int layoutId,
			List<RecipeUpdate> data) {
		super(context, layoutId, data);
		this.context = context;
		this.layoutId = layoutId;
		this.data = data;
		
		/*if(data.size() == 0){
			firstRecipe.setId("1");
			firstRecipe.setDownloaded(true);
			firstRecipe.setName("Recipe Pack 1");
			firstRecipe.setPurchased(true);
			
			ArrayList<Pair<String, String>> items = new ArrayList<Pair<String, String>>();
			items.add(new Pair<String, String>("Carrot Halwa", ""));
			items.add(new Pair<String, String>("Chapatti (Plain)", ""));
			items.add(new Pair<String, String>("Curried Mince", ""));
			items.add(new Pair<String, String>("Easy Mango Pickle", ""));
			items.add(new Pair<String, String>("Fried Bitter Gourd", ""));
			items.add(new Pair<String, String>("Hot Stuff", ""));
			items.add(new Pair<String, String>("Masala Vadais", ""));
			items.add(new Pair<String, String>("Ox Tail Coconut Stew", ""));
			items.add(new Pair<String, String>("Pork Vindaloo", ""));
			items.add(new Pair<String, String>("Yoghurt Rice", ""));
			
			firstRecipe.setItems(items);
			
			data.add(firstRecipe);
		}*/
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(layoutId, parent, false);

			holder = new ViewHolder();

			holder.textviewPrice = (TextView) convertView
					.findViewById(R.id.tv_price);
			holder.textviewName = (TextView) convertView
					.findViewById(R.id.tv_name);
			holder.recipeItem = new TextView[10];
			holder.recipeItem[0] = (TextView) convertView
					.findViewById(R.id.tv_item1);
			holder.recipeItem[1] = (TextView) convertView
					.findViewById(R.id.tv_item2);
			holder.recipeItem[2] = (TextView) convertView
					.findViewById(R.id.tv_item3);
			holder.recipeItem[3] = (TextView) convertView
					.findViewById(R.id.tv_item4);
			holder.recipeItem[4] = (TextView) convertView
					.findViewById(R.id.tv_item5);
			holder.recipeItem[5] = (TextView) convertView
					.findViewById(R.id.tv_item6);
			holder.recipeItem[6] = (TextView) convertView
					.findViewById(R.id.tv_item7);
			holder.recipeItem[7] = (TextView) convertView
					.findViewById(R.id.tv_item8);
			holder.recipeItem[8] = (TextView) convertView
					.findViewById(R.id.tv_item9);
			holder.recipeItem[9] = (TextView) convertView
					.findViewById(R.id.tv_item10);
			holder.layoutDescription = (LinearLayout) convertView
					.findViewById(R.id.layout_description);
			holder.layoutDownload = (LinearLayout) convertView
					.findViewById(R.id.layout_download);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		RecipeUpdate item = data.get(position);
		holder.textviewPrice.setText(item.getPrice());
		holder.textviewName.setText(item.getName());
		for (int i = 0; i < item.getItems().size() && i < 10; i++) {
			String recipe = item.getItems().get(i).first;
			if (recipe != null) {
				holder.recipeItem[i].setText(recipe);
			} else {
				holder.recipeItem[i].setText("");
			}

		}

		holder.layoutDescription.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				final RecipeUpdate item = data.get(position);
				
				if (!Shared.dbAdapter.isPackageAvailable(item.getId())) {
					DownloadRecipeTask task = new DownloadRecipeTask(
							((ActivityUpdate) context).getParent(), item, !item.isPurchased()){
						@Override
						protected void onPostExecute(Void result) {
							super.onPostExecute(result);
							
							goToDetailRecipe(item);
						}
					};
					task.execute();
				}else{
					goToDetailRecipe(item);
				}
				
			}
		});

		holder.layoutDownload.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				RecipeUpdate item = data.get(position);
				if (!item.isPurchased()) {
					((ActivityUpdate) getContext())
					.lauchPurchase(getItem(position));
					
				} else if (!Shared.dbAdapter.isPackageAvailable(item.getId())) {
					DownloadRecipeTask task = new DownloadRecipeTask(
							((ActivityUpdate) context).getParent(),
							getItem(position), false);
					task.execute();
				}else{
					Toast.makeText(getContext(), "Package is available.", Toast.LENGTH_SHORT).show();
				}
			}
		});

		holder.textviewName.setText(data.get(position).getName());

		convertView.setClickable(false);
		
		return convertView;
	}

	private void goToDetailRecipe(RecipeUpdate item){
		Intent intent = new Intent(((NavigationActivity) context)
				.getParent(), ActivityRecipeUpdateDescription.class);
		intent.putExtra("package_id", item.getId());
		
		((NavigationActivity) context).goNextHistory(
					"Recipe Update Description", intent);
	}
	
	static class ViewHolder {
		TextView textviewName;
		TextView textviewPrice;
		TextView[] recipeItem;
		LinearLayout layoutDescription;
		LinearLayout layoutDownload;
	}

}